﻿using ShiQuan.IotDevice.Entity;
using ShiQuan.Utility;
using ShiQuan.WebControl;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace ShiQuan.IotDevice.Business
{
    /// <summary>
    /// 设备传感器
    /// </summary>
    public class IotDeviceSensorBLL:BaseIotDeviceBLL<IotDeviceSensorEntity>
    {
        /// <summary>
        /// 获取树状列表
        /// </summary>
        /// <param name="queryJson"></param>
        /// <param name="root"></param>
        /// <returns></returns>
        public List<TreeModel> GetTreeList(string deviceId,bool root = true)
        {
            var data = this.GetList(my=>my.RefDeviceId == deviceId).OrderBy(my=>my.Code).ToList();

            var treeList = new List<TreeModel>();
            TreeModel rootModel = null;
            if (root)
            {
                rootModel = new TreeModel();
                bool hasChildren = data.Count() == 0 ? false : true;
                rootModel.id = "-1";
                rootModel.text = "所有传感器";
                rootModel.value = "-1";
                rootModel.isexpand = true;
                rootModel.complete = true;
                rootModel.hasChildren = hasChildren;
                rootModel.parentId = "0";
                //tree.icon = item.Icon;
                treeList.Add(rootModel);
            }
            foreach (IotDeviceSensorEntity item in data)
            {
                TreeModel tree = new TreeModel();
                tree.id = item.Id;
                tree.text = item.Code +"_"+item.Name;
                tree.value = item.Id;
                tree.isexpand = true;
                tree.complete = true;
                tree.hasChildren = false;
                if (rootModel != null)
                    tree.parentId = rootModel.id;
                else
                    tree.parentId = "0";
                //tree.icon = item.Icon;
                treeList.Add(tree);
            }
            return treeList;
        }
        /// <summary>
        /// 设备安装
        /// </summary>
        /// <param name="deviceId"></param>
        /// <returns></returns>
        public IotDeviceSensorEntity GetNewEntity(string deviceId)
        {
            IotDeviceSensorEntity entity = new IotDeviceSensorEntity();
            //entity.Code = DateTime.Now.ToString("HHmmss");
            if (string.IsNullOrEmpty(deviceId) == false)
            {
                IotDeviceInfoEntity deviceInfo = new IotDeviceInfoBLL().GetEntity(deviceId);
                if (deviceInfo != null)
                {
                    entity.RefDeviceId = deviceInfo.Id;
                    entity.DeviceName = deviceInfo.Name;
                }
            }
            return entity;
        }
        /// <summary>
        /// 查询过滤
        /// </summary>
        /// <param name="expression"></param>
        /// <param name="queryJson"></param>
        /// <returns></returns>
        protected override Expression<Func<IotDeviceSensorEntity, bool>> QueryStringToExpression(Expression<Func<IotDeviceSensorEntity, bool>> expression, string queryJson)
        {
            if (string.IsNullOrEmpty(queryJson))
                return expression;
            var queryParam = queryJson.ToJObject();
            if (queryParam["RefDeviceId"].IsEmpty() == false && queryParam["RefDeviceId"].ToString() != "-1")
            {
                string keyword = queryParam["RefDeviceId"].ToString();
                expression = expression.And(my => my.RefDeviceId == keyword);
            }
            if (!queryParam["condition"].IsEmpty() && !queryParam["keyword"].IsEmpty())
            {
                string condition = queryParam["condition"].ToString();
                string keyword = queryParam["keyword"].ToString();
                switch (condition)
                {
                    default: break;
                }
            }
            return expression;
        }
    }
}
